package com.opencvjava.lessons.imgproc;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.opencvjava.support.util.CvUtils.imshow;
import static com.opencvjava.support.util.CvUtils.mat;
import static org.opencv.core.Core.merge;
import static org.opencv.core.Core.split;
import static org.opencv.imgproc.Imgproc.COLOR_BGR2GRAY;
import static org.opencv.imgproc.Imgproc.cvtColor;
import static org.opencv.imgproc.Imgproc.equalizeHist;

@Service
public class L17_HistogramEqualization {
    public void test() {
        Mat src = mat("lena.jpg");
        if (src.empty()) {
            throw new RuntimeException("图片读取失败");
        }
        Mat dst = new Mat();
        cvtColor(src, src, COLOR_BGR2GRAY);
        // 必须是单通道，否则会报异常
        equalizeHist(src, dst);
        imshow("Source gray image", src);
        imshow("Equalized gray Image", dst);
        L18_HistogramCalc.HistogramAxes(src);
        L18_HistogramCalc.HistogramAxes(dst);

        src = mat("lena.jpg");
        List<Mat> bgrPlanes = new ArrayList<>();
        split(src, bgrPlanes);
        for (Mat bgr : bgrPlanes) {
            equalizeHist(bgr, bgr);
        }
        merge(bgrPlanes, dst);
        imshow("Source image", src);
        imshow("Equalized Image", dst);
        L18_HistogramCalc.HistogramAxes(src);
        L18_HistogramCalc.HistogramAxes(dst);
    }
}
